home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Devil's Cubes 1.0.1 / source / Devil’s Cubes ƒ / Cube code / cube load-save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.2 KB  |  180 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        cube load-save.c
  4.  
  5. Purpose:    This module handles actually loading and saving games.
  6.  
  7.  
  8. Devil’s Cubes -- a simple cubes puzzle
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "Script.h"
  29. #include "cube load-save.h"
  30. #include "cube.h"
  31. #include "cube meat.h"
  32. #include "cube files.h"
  33. #include "msg graphics.h"
  34. #include "msg menus.h"
  35. #include "msg dialogs.h"
  36. #include "msg sounds.h"
  37. #include "msg environment.h"
  38. #include "msg prefs.h"
  39.  
  40. FSSpec            gameFile;
  41. Boolean            deleteTheThing;
  42.  
  43. void LoadSaveDispatch(Boolean isLoad, Boolean useOldGame)
  44. {
  45.     if (isLoad)
  46.     {
  47.         if (GetSourceFile(&gameFile, useOldGame))
  48.             GetSavedGame(&gameFile);
  49.     }
  50.     else
  51.     {
  52.         useOldGame=useOldGame&&(gameFile.name[0]!=0x00);
  53.         if (useOldGame ? TRUE : GetDestFile(&gameFile, &deleteTheThing))
  54.             SaveGame(gameFile);
  55.     }
  56. }
  57.  
  58. void SaveGame(FSSpec gameFile)
  59. {
  60.     int                i,j;
  61.     int                buffer[25];
  62.     int                thisFile;
  63.     long            count;
  64.     int                iter;
  65.     OSErr            theError;
  66.     
  67.     for (i=0; i<4; i++)
  68.         for (j=0; j<6; j++)
  69.             buffer[i*6+j]=Cube[i][j];
  70.     
  71.     buffer[24]=0;
  72.     for (i=0; i<24; i++)
  73.         buffer[24]+=buffer[i];
  74.     
  75.     if (deleteTheThing)
  76.     {
  77.         if (gHasFSSpecs)
  78.             FSpDelete(&gameFile);
  79.         else
  80.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  81.     }
  82.     FlushVol(0L, gameFile.vRefNum);
  83.     
  84.     if (gHasFSSpecs)
  85.         theError=FSpCreate(&gameFile, 'Dcbe', 'DcSG', smSystemScript);
  86.     else
  87.         theError=HCreate(gameFile.vRefNum, gameFile.parID,
  88.                         gameFile.name, 'Dcbe', 'DcSG');
  89.     FlushVol(0L, gameFile.vRefNum);
  90.     
  91.     if (theError==noErr)
  92.     {
  93.         if (gHasFSSpecs)
  94.             theError=FSpOpenDF(&gameFile, fsRdWrPerm, &thisFile);
  95.         else
  96.             theError=HOpen(gameFile.vRefNum, gameFile.parID,
  97.                             gameFile.name, fsRdWrPerm, &thisFile);
  98.     }
  99.     if (theError==noErr)
  100.     {
  101.         count=50L;
  102.         SetEOF(thisFile, 50L);
  103.         SetFPos(thisFile, 1, 0L);
  104.         theError=FSWrite(thisFile, &count, buffer);
  105.         FSClose(thisFile);
  106.         FlushVol(0L, gameFile.vRefNum);
  107.     }
  108.     if (theError!=noErr)
  109.     {
  110.         if (gHasFSSpecs)
  111.             FSpDelete(&gameFile);
  112.         else
  113.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  114.         ParamText("\pSorry, an error occurred trying to write the game to disk.","","","");
  115.         PositionDialog('ALRT', generalAlert);
  116.         StopAlert(generalAlert,0L);
  117.     }
  118.     else
  119.         deleteTheThing=TRUE;
  120. }
  121.  
  122. void GetSavedGame(FSSpec *gameFile)
  123. {
  124.     int                thisFile;
  125.     int                checksum;
  126.     long            count;
  127.     int                i,j;
  128.     int                buffer[25];
  129.     OSErr            theError;
  130.  
  131.     if (gHasFSSpecs)
  132.         theError=FSpOpenDF(gameFile, fsRdPerm, &thisFile);
  133.     else
  134.         theError=HOpen(gameFile->vRefNum, gameFile->parID, gameFile->name, fsRdPerm, &thisFile);
  135.  
  136.     if (theError==noErr)
  137.     {
  138.         count=50L;
  139.         SetFPos(thisFile, 1, 0L);
  140.         theError=FSRead(thisFile, &count, buffer);
  141.         FSClose(thisFile);
  142.     }
  143.     else
  144.     {
  145.         ParamText("\pSorry, an error occurred while trying to read the game from disk.","","","");
  146.         PositionDialog('ALRT', generalAlert);
  147.         StopAlert(generalAlert,0L);
  148.         return;
  149.     }
  150.     
  151.     if (theError==noErr)
  152.     {                
  153.         checksum=0;
  154.         for (i=0; i<24; i++)
  155.                 checksum+=buffer[i];
  156.  
  157.         if (checksum!=buffer[24])
  158.         {
  159.             ParamText("\pSorry, this game file is damaged or has been altered.","","","");
  160.             PositionDialog('ALRT', generalAlert);
  161.             StopAlert(generalAlert, 0L);
  162.         }
  163.         else
  164.         {
  165.             for (i=0; i<4; i++)
  166.                 for (j=0; j<6; j++)
  167.                     Cube[i][j]=buffer[i*6+j];
  168.             deleteTheThing=TRUE;
  169.             OpenMainWindow();
  170.             AdjustMenus();
  171.         }
  172.     }
  173.     else
  174.     {
  175.         ParamText("\pSorry, an error occurred while trying to read the game from disk.","","","");
  176.         PositionDialog('ALRT', generalAlert);
  177.         StopAlert(generalAlert,0L);
  178.     }
  179. }
  180.